<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="multipart/form-data; charset=utf-8" />
    <title>区域基站修改</title>
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <link href="../../layui/css/iconfont/iconfont.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <script src="../../js/jquery-ui-1.10.4.custom.min.js"></script>
    <script type="text/javascript">

        layui.use(['form', 'layer', 'jquery', 'element', 'upload', 'table'], function () {
            var table = layui.table;
            var form = layui.form;
            var $ = layui.$;
            var layer = layui.layer;
            var upload = layui.upload;
            var user;
            var mgrId = getUrlParam("mgrId");
            var cityId = getUrlParam("cityId");
            var importStationType="";//导入基站csv文件类型（追加or覆盖）
            var copyMgrId;
            var mgrNameId;//项目简称id
            
            var options = {
                elem: "#table",//表格id
                id: "modelReload", //id
                url: '/smsBaseStation/find',
                method: 'post',
                where: {
		                        mgrId: mgrId,
		                        cityId:cityId,
		                    },
                request: {
                    pageName: 'page',
                    limitName: 'limit',
                },
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    msgName: 'msg',
                    countName: 'count',
                    dataName: 'data'
                },
                page: true,
                count: "count",
                limit: 10,
                limits: [10, 20, 30, 40, 50],
                layout: ['prev', 'page', 'next'],
                prev: "上一页",
                next: "下一页",
                cols: [[ //表头
                     {field: 'regionId', title: '区域编码'}
                    , {field: 'regionName', title: '区域名称'}
                    , {field: 'stationId', title: '基站编码'}
                    , {field: 'stationName', title: '基站名称'}
                ]]
            };
            table.render(options);
            
            upload.render({
				elem : '#importAdd,#importCover',
				url : '/template2/modifyStation',
				auto : false, //关闭自动上传
				accept : 'file', //普通文件
				exts : 'csv', //只允许上传csv文件 //,multiple: true
				bindAction : '#uploadFile',
				before : function() {
		            var param = {
		            		mgrId: mgrId,
		                    cityId: cityId,
		                    importType:importStationType
		                };
		                //var json = JSON.stringify(param);
					layer.load(); //上传loading
					this.data = param;
				},
				done : function(obj) {
	
					layer.closeAll('loading'); //关闭loading
					if (obj.code == 0) {
						var data = obj.data;
						var num = data.toString().split(",");
						layer.confirm('模板添加成功，并成功导入' + data[0] + '个区域,' + data[1] + '个基站', {
							btn: ['确定'] //按钮
						}, function () {
							location.href="smsTemplateManage2.html";
						});
					}
					if (obj.code > 0) {
						layer.msg(obj.msg);
					}
	
				},
				error : function(index, upload) {
					layer.closeAll('loading'); //关闭loading
					layer.msg("上传过程中出现异常，请重试");
				}
			});
			//获取复制的项目ID
			 $('#copy').on('click',function(){
			 	copyMgrId = $("#mgrId_third").val();
			 	mgrNameId = $("#mgrName1").val();
			 	if((''!=mgrNameId && null!=mgrNameId)&&(''!=copyMgrId && null!=copyMgrId))
			 	{layer.confirm('项目名称与项目简称只需选择一个', {
						btn: ['确定'] //按钮
						}, function () {
                         location.href = "stationImport.html?mgrId="+mgrId+"&cityId="+cityId;
			         });
					}else{		 	
			 	if((''!=mgrNameId && null!=mgrNameId)){			
    					var url = '/util/findMgrByName';
    					var param = {
    						mgrName : mgrNameId
    					};
    					$.ajax({
    						type : 'post',
    						async : false,
    						url : url,
    						data : param,
    						success : function(json) {
    							 data = json.data; 
    							 console.log(data.id);	  						
    						 mgrNameId=data.id;	    		
    						},
    						error : function() {
    							layer.msg('系统异常，请联系管理员处理!');
    						}
    					});
    					copyMgrId=mgrNameId;  
    					console.log(copyMgrId); 					
			 	}
			 	layer.confirm('已复制对应基站', {
						btn: ['确定'] //按钮
					});
			 	}		 	
			 });
			 $('#upload2').on('click', function () {
				copyStation();
			});
			//判断是覆盖导入还是追加导入
			$(".importStation").click(function(){
				importStationType = $(this).attr('id');
			});
			$('#upload').on('click', function () {
				var choose = $("span.layui-upload-choose").text();
				if(choose!=null && choose!=''){
					$('#uploadFile').trigger("click");
				}else{
					layer.msg("请先选择要导入的csv文件");
				}
			});
			$(".importType").click(function(){
                 	var importType = $("input[name='importType']:checked").val();
                 	if(importType==1){
                 		$(".copyStation").css("display","none");
                 		$(".import").css("display","");
                 		$("#upload").css("display","");
	                	$("#upload2").css("display","none");
                 	}else if(importType==2){
                 		$(".copyStation").css("display","");
                 		$(".import").css("display","none");
                 		$("#upload").css("display","none");
	                	$("#upload2").css("display","");
                 	}
        	});
        	function copyStation(){
	        	if (isNull(copyMgrId)) {
		          layer.msg("未选择要复制的项目");
		          return false;
		        }
        		layer.confirm('确认模板信息无误吗？是否需要再次查看', {
					btn : [ '不需要', '需要' ] //按钮
				}, function(){
					var url = '/template2/copyStation';
	                    		var params = {
	                    			copyMgrId:copyMgrId,
	                    			mgrId:mgrId,         
	                    		}
	                    		var index = layer.load(1, {shade: [0.1,'#fff'] });
	                    		$.ajax({
	                    			type: 'post',
				                    data: params,
				                    url: url,
				                    async: false,
				                    success: function(obj){
				                    	layer.close(index);
				                    	if(obj.data>0){
				                    		layer.confirm('修改成功，并复制'+obj.data+'个基站', {
							                    btn: ['确定'] //按钮
							                }, function () {
							                    location.href="smsTemplateManage2.html";
							                });
				                    	}else{
				                    		layer.confirm('复制项目失败，如需复制项目，请点击确定留在此页面', {
							                    btn: ['确定','退出'] //按钮
							                }, function () {
							                    layer.close(layer.index);
							                }, function () {
							                    location.href="smsTemplateManage2.html";
							                });
				                    	}
				                    },error: function(){
				                    	layer.close(index);
	                    				layer.msg('系统异常，请联系管理员处理!');
				                    }
	                    		});
				});
        	}
            //身份识别
            $(document).ready(function () {
                if(!isNull(cityId)){
                	$("#cityId").val(cityId);
                    $("#cityId").attr("disabled", true);
                    var data = selectMgrInfo(cityId);
                    appendMgr(data);
                    form.render();
                }
                if(!isNull(mgrId)){
					var select = "dd[lay-value='"+mgrId+"']";
					$('#mgrId').siblings("div.layui-form-select").find('dl').find(select).click();
					$("#mgrId").attr("disabled", "disabled");
					form.render();
                }
                $(".copyStation").css("display","none");
            });

            //根据城市ID查询项目
            function selectMgrInfo(countyId) {
                var data;
                //var url = '/util/findByCityId?cityId=' + cityId;
                var url = '/util/findByMgrId?countyId=' + countyId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }  //监听项目
            form.on('select(cityId)', function (data) {
                var data = selectMgrInfo(data.value);
                appendMgr(data);
            });

            //动态拼接项目
            function appendMgr(data) {
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                    }
                    $('#mgrId').html(options);
                } else {
                    $('#mgrId').html('<option value="">该地市下没有项目</option>');
                }
                form.render();
            }
            //根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
			//监听第三步中的地市、区县、项目
		//监听区县
            form.on('select(cityId_third)', function (data) {
                var data2 = selectCountyId(data.value);
                appendCountyThird(data2);
                var data1 = selectMgrInfo(data.value);
                appendMgrThird(data1);
            });
			
			 //监听项目
            form.on('select(countyId_third)', function (data) {
            	if(data.value==null||data.value==''){
	            		var cityId = $("#cityId_third").val();
	            		var data1 = selectMgrInfo(cityId);
	            		appendMgrThird(data1);
	            	}else{
	            		var data1 = selectMgrInfo(data.value);
	                	appendMgrThird(data1);
	            	}
            });
            //动态拼接区县
            function appendCountyThird(data){
                if (data.length != null) {
                    var options = '<option value="">请先选择地市</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId_third').html(options);
                } else {
                    $('#countyId_third').html('该地市下没有区县');
                }
                form.render();
            }
			
			 //动态拼接项目
            function appendMgrThird(data) {
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                    }
                    $('#mgrId_third').html(options);
                } else {
                    $('#mgrId_third').html('<option value="">该区县下没有项目</option>');
                }
                form.render();
            }
        //监听第三步中的地市、区县、项目 end
        
        		
    		$("#mgrName1").autocomplete({
       source : function( request, response ) {
        var key = $("#mgrName1").val();
        var cityId=$("#cityId").val();
        var countyId=$("#countyId").val();
        var url = '/util/selectMgrNameLike';
        var param = {
            cityId : cityId,
            countyId : countyId,
         key : key
        };
                 $.ajax({
         type : 'post',
         async : false,
         url : url,
         data : param,
         success : function(json) {
          var data = json.data;
          source = [];
          for (var i = 0; i < data.length; i++) {
           source[i] = data[i].name;
          }
          console.log(source);
          response( source );
         },
         error : function() {
          layer.msg('系统异常，请联系管理员处理!');
         }
        }
        );
            },
       messages : { //message设置不出现匹配关键字的结果
        noResults : '',
        results : function() {
        }
       }
      });
        });
    </script>
</head>
<body>
<div class="layui-content-body">
    <div class="scroll-box">
        <div class="breadcrumb-box">
                <span class="layui-breadcrumb" lay-separator="|">
                     <a href="javascript:returnIndex2()">首页&nbsp;</a>
                  <a href="smsTemplateManage2.html">短信项目模板配置管理</a>
                  <a href="">区域基站导入&nbsp;</a>
                </span>
        </div>
        <div class="content-box">
            <div class="form-select-box">
                <div class="layui-form layui-form-pane">
                    <div class="layui-upload">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">省份</label>
                            <div class="layui-input-block">
                                <input type="text" name="province" lay-verify="required" value="江苏"
                                       autocomplete="off" class="layui-input" disabled>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">地市</label>
                            <div class="layui-input-block">
                                <select id="cityId" name="cityId" lay-filter="cityId" id="cityId">
                                    <option value="">请选择</option>
                                    <option value="11">苏州</option>
                                    <option value="12">淮安</option>
                                    <option value="13">宿迁</option>
                                    <option value="14">南京</option>
                                    <option value="15">连云港</option>
                                    <option value="16">徐州</option>
                                    <option value="17">常州</option>
                                    <option value="18">镇江</option>
                                    <option value="19">无锡</option>
                                    <option value="20">南通</option>
                                    <option value="21">泰州</option>
                                    <option value="22">盐城</option>
                                    <option value="23">扬州</option>
                                    <option value="25">省级</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item mgrName">
                        <div class="layui-inline">
                            <label class="layui-form-label">项目名称</label>
                            <div class="layui-input-block">
                                <select name="mgrId" lay-filter="mgrId" id="mgrId">
                                    <option value="">请先选择地市</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item importType">
					                    	<label class="layui-form-label" style="width:120px">基站来源</label>
					                        <div class="layui-input-block">
					                        	<input class="radioItem" lay-filter="gis" type="radio" name="importType" title="导入基站" onclick="ChooseRadio(this)" value="1" checked="checked">
					                            <input class="radioItem" lay-filter="gis" type="radio" name="importType" title="复制其他项目基站" onclick="ChooseRadio(this)" value="2">
					                        </div>
					</div>
                    		<div class="layui-form-item copyStation">
								<div class="layui-inline ">
									<label class="layui-form-label">地市</label>
									<div class="layui-input-block" style="width:150px">
										<select name="cityId_third" lay-filter="cityId_third" id="cityId_third">
											<option value="">请选择</option>
											<option value="11">苏州</option>
											<option value="12">淮安</option>
											<option value="13">宿迁</option>
											<option value="14">南京</option>
											<option value="15">连云港</option>
											<option value="16">徐州</option>
											<option value="17">常州</option>
											<option value="18">镇江</option>
											<option value="19">无锡</option>
											<option value="20">南通</option>
											<option value="21">泰州</option>
											<option value="22">盐城</option>
											<option value="23">扬州</option>
											<option value="25">省级</option>
										</select>
									</div>
								</div>
								<div class="layui-inline countyId">
		                            <label class="layui-form-label">区县</label>
		                            <div class="layui-input-block" style="width:150px">
		                                <select name="countyId_third" lay-filter="countyId_third" id="countyId_third">
		                                    <option value="">全部</option>
		                                </select>
		                            </div>
		                        </div>
		                        <div class="layui-inline mgrId">
		                            <label class="layui-form-label">项目名称</label>
		                            <div class="layui-input-block" style="width:150px">
		                                <select name="mgrId_third" lay-filter="mgrId_third" id="mgrId_third">
		                                    <option value="">请选择</option>
		                                </select>
		                            </div>
		                        </div>
		                        	<div class="layui-inline">
                            <label class="layui-form-label">项目简称</label>
                            <div class="layui-input-block" style="width:150px">
                                <input type="text"  name="mgrName1" id="mgrName1" lay-verify="required" placeholder="请输入"
                                            autocomplete="off" class="layui-input" />
                            </div>
                        </div>
		                        <div class="layui-inline">
		                            <div class="layui-block">
				                            <button class="layui-btn layui-btn-normal" id="copy">复制</button>
				                            <label id="copyMgrId" style="visibility:hidden;">123</label>
				                	</div>
		                        </div>
							</div>
							
                            <div class="layui-form-item import">
        						<label class="layui-form-label" style="width: 120px;">区域基站导入</label>

                                <div class="layui-inline">
                                    	<button type="button" class="layui-btn importStation" id="importCover"><i class="layui-icon"></i>覆盖导入</button>
                                </div>
                                <div class="layui-inline">
                                    	<button type="button" class="layui-btn importStation" id="importAdd"><i class="layui-icon"></i>追加导入</button>
                                </div>
                                <br class="layui-form-item"> <span><a
									href="/smsBaseStation/apidownload"><u>基站导入帮助手册</u></a></span></br> <span><a
									href="/smsBaseStation/modeldownload"><u>基站导入模板下载</u></a></span>
        					</div>
        					<div class="layui-form-item">
								<button class="layui-btn" id="upload">完成</button>
							</div>
							<div class="layui-form-item">
								<button class="layui-btn" id="upload2" style="display:none;">完成</button>
							</div>

							<div class="layui-form-item" style="display:none;">
								<button class="layui-btn" id="uploadFile">上传</button>
							</div>
                        </div>
                    </div>
                </div>
                <div>
                    <h4 class="tips-h4"><i class="iconfont icon-xinxi"></i>提示：基站信息以呈批文件为准！支持重新导入
                    </h4>
                </div>
                 <div class="layui-table">
		                	<table id="table" lay-filter="tableElement"></table>
		           	 	</div>
            </div>
        </div>
    </div>
</div>

</body>
</html>